package com.example.controller;

import com.example.common.BaseResponse;
import com.example.common.ErrorCode;
import com.example.common.ResultUtils;
import com.example.exception.BusinessException;
import com.example.model.hotel.Hotel;
import com.example.model.hotel.HotelAddRequest;
import com.example.model.hotel.HotelRoomCount;
import com.example.model.hotel.HotelUpdateRequest;
import com.example.model.user.User;
import com.example.service.HotelService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.example.contant.UserConstant.*;

@Slf4j
@RequestMapping("/hotel")
@RestController
public class HotelController {

    @Resource
    private HotelService hotelService;

    /**
     * 新增酒店
     * @param hotelAddRequest 新增酒店实体
     * @param request HttpServletRequest
     * @return 新增酒店id
     */
    @PostMapping("/add")
    public BaseResponse<Long> addHotel(@RequestBody HotelAddRequest hotelAddRequest, HttpServletRequest request) {
        if (!isAdmin(request)){
            throw new BusinessException(ErrorCode.NO_AUTH);
        }
        Long id=hotelService.addHotel(hotelAddRequest);
        log.info("hotel add id:{}", id);
        if (id==null){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"添加失败");
        }
        return ResultUtils.success(id);

    }

    /**
     * 修改酒店信息
     * @param hotelUpdateRequest 修改酒店信息实体
     * @param request HttpServletRequest
     * @return 是否成功
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateHotel(@RequestBody HotelUpdateRequest hotelUpdateRequest, HttpServletRequest request){
        if (!isAdmin(request)){
            throw new BusinessException(ErrorCode.NO_AUTH);
        }
        Boolean result=hotelService.update(hotelUpdateRequest);
        if(!result)
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * 是否为管理员
     *
     * @param request
     * @return
     */
    private boolean isAdmin(HttpServletRequest request) {
        Object userObj = request.getSession().getAttribute(USER_LOGIN_STATE);
        User user = (User) userObj;
//        if (user == null || (user.getUserRole() != ADMIN_ROLE && user.getUserRole() != SUPER_ADMIN_ROLE))
//            return false;
//        return true;
        if (userObj == null || !(userObj instanceof User)) {
            // userObj 是 null 或者不是 User 类型，返回 false
            return false;
        }
        // 检查用户角色是否为 1 或 2
        return user.getUserRole() == ADMIN_ROLE || user.getUserRole() == SUPER_ADMIN_ROLE;
    }

    /**
     * 查询酒店
     * @return 酒店集合
     */
    @GetMapping("/search")
//    public BaseResponse<List<Hotel>> getHotels(@RequestBody Hotel hotel) {
    public BaseResponse<List<Hotel>> getHotels(
            @RequestParam(value = "hotelName", required = false, defaultValue = "") String hotelName,
            @RequestParam(value = "city", required = false, defaultValue = "") String city,
            @RequestParam(value = "address", required = false, defaultValue = "") String address,
            @RequestParam(value = "hotelStatus", required = false, defaultValue = "") Integer hotelStatus ){
        Hotel hotel=new Hotel();
        hotel.setHotelName(hotelName);
        hotel.setCity(city);
        hotel.setAddress(address);
        hotel.setHotelStatus(hotelStatus);
        List<Hotel> hotelList;
        if (hotel == null){
            hotelList=hotelService.getAllHotels();
            return ResultUtils.success(hotelList);
        }
        hotelList=hotelService.getHotels(hotel);
        return ResultUtils.success(hotelList);
    }

    /**
     * 获取酒店房间数
     * @return 酒店房间数集合
     */
    @GetMapping("/count")
    public BaseResponse<List<HotelRoomCount>> getHotelCount() {
        List<HotelRoomCount> roomCountList=hotelService.getHotelCount();
        if (roomCountList==null){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        }
        return ResultUtils.success(roomCountList);
    }

}
